package com.dmall.algorithm.chapter01;

public class Leetcode206V1 {

    private static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode reverseList(ListNode head) {
        ListNode prev = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        return prev;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        ListNode cur = head;
        for (int i = 2; i <= 5; i++) {
            cur.next = new ListNode(i);
            cur = cur.next;
        }

        ListNode curr = head;
        StringBuilder sb = new StringBuilder();
        while (curr != null) {
            sb.append(curr.val).append("->");
            curr = curr.next;
        }
        sb.append("NULL");
        System.out.println(sb);

        Leetcode206V1 leetcode206V1 = new Leetcode206V1();
        ListNode newNode = leetcode206V1.reverseList(head);
        curr = newNode;
        StringBuilder sb1 = new StringBuilder();
        while (curr != null) {
            sb1.append(curr.val).append("->");
            curr = curr.next;
        }
        sb1.append("NULL");
        System.out.println(sb1);
    }
}
